package mobi.greenplate.client.converter;

/**
 *
 * @author dmakariev
 */
class XmlBuilder {

    static XmlBuilder newRoot(String tagName) {
        return new XmlBuilder(tagName);
    }

    private XmlBuilder(String rootTagName) {
        this.rootTagName = rootTagName;
        appendStartTag(stringBuilder, rootTagName);
    }
    private final String rootTagName;
    private final StringBuilder stringBuilder = new StringBuilder();

    public XmlBuilder startTag(String tagName) {
        appendStartTag(stringBuilder, tagName);
        return this;
    }

    public XmlBuilder endTag(String tagName) {
        appendEndTag(stringBuilder, tagName);
        return this;
    }

//    public XmlBuilder addText(String text){
//        stringBuilder.append(text);
//        return this;
//    }

    public XmlBuilder addTag(String tagName, String text) {
        appendStartTag(stringBuilder, tagName);
        stringBuilder.append(text);
        appendEndTag(stringBuilder, tagName);
        return this;
    }

    public String buildXml() {
        appendEndTag(stringBuilder, rootTagName);
        final String finalXml = stringBuilder.toString();
        return finalXml;
    }

    private static void appendStartTag(StringBuilder sb, String tagName) {
        sb.append('<').append(tagName).append('>');
    }

    private static void appendEndTag(StringBuilder sb, String tagName) {
        sb.append('<').append('/').append(tagName).append('>');
    }
}
